import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/picker_style.dart';
import 'package:flutter_pickers/time_picker/model/pduration.dart';
import 'package:flutter_pickers/time_picker/model/suffix.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:flutter_pickers/time_picker/model/date_mode.dart';
import 'package:library_base/utils/date_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/utils/get_util.dart';

class TimePickDialog extends StatefulWidget {
  const TimePickDialog({
    Key? key,
  }) : super(key: key);

  @override
  State<TimePickDialog> createState() => _TimePickDialogState();
}

class _TimePickDialogState extends State<TimePickDialog> {
  DateTime? currentTime;

  var loadingTime = '';

  Function(String)? filter;

  Function()? reset;

  @override
  void initState() {
    super.initState();
    currentTime = Get.arguments['currentTime'];
    filter = Get.arguments['filter'];
    reset = Get.arguments['reset'];
    if (currentTime != null) {
      loadingTime = DateUtil.formatDate(currentTime, format: DataFormats.zh_y_mo_d);
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        GetHelper.back();
      },
      child: Material(
        color: Colors.transparent,
        child: Container(
          color: Colors.transparent,
          child: GestureDetector(
            onTap: () {},
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  width: 0.88.sw,
                  decoration: rs8,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      gapH36,
                      SizedBox(
                        width: double.infinity,
                        child: Stack(
                          children: [
                            Container(
                                alignment: Alignment.center,
                                child: Text('配送时间', style: normalBoldTextColor32)),
                            Positioned(
                              right: 24.w,
                              child: GestureDetector(
                                onTap: () => Get.back(),
                                child: LocalImage('common_close',
                                    package: Constant.packLib(), width: 26.r, height: 26.r),
                              ),
                            )
                          ],
                        ),
                      ),
                      gapH24,
                      line1,
                      gapH24,
                      GestureDetector(
                        onTap: () {
                          showDateTimePicker();
                        },
                        child: Container(
                            alignment: Alignment.center,
                            width: double.infinity,
                            margin: edgeLR24,
                            decoration: BoxDecoration(
                              color: ThemeUtil.getTheme().secondaryBgColor(),
                              borderRadius: const BorderRadius.all(Radius.circular(6.0)),
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Expanded(
                                  child: Container(
                                    padding: EdgeInsets.symmetric(vertical: 30.w, horizontal: 24.w),
                                    alignment: Alignment.center,
                                    // width: double.infinity,
                                    child: Text(
                                      currentTime == null ? '选择配送时间' : loadingTime,
                                      style: currentTime == null
                                          ? TextStyles.textSixthColor28
                                          : TextStyles.textNormal28,
                                      textAlign: TextAlign.start,
                                    ),
                                  ),
                                ),
                              ],
                            )),
                      ),
                      gapH24,
                      Container(
                        padding: edgeLR24,
                        child: Row(
                          children: [
                            Expanded(
                              child: GradientButton(
                                ThemeUtil.getTheme(),
                                radius: const BorderRadius.only(
                                    topLeft: Radius.circular(50), bottomLeft: Radius.circular(50)),
                                colors: ThemeUtil.getTheme().buttonLeftBgColor(),
                                height: 80.w,
                                text: MultiLanguage.reset.tr,
                                onPressed: () {
                                  GetHelper.back();
                                  reset?.call();
                                },
                              ),
                            ),
                            Expanded(
                              child: GradientButton(
                                ThemeUtil.getTheme(),
                                radius: const BorderRadius.only(
                                    topRight: Radius.circular(50),
                                    bottomRight: Radius.circular(50)),
                                height: 80.w,
                                text: MultiLanguage.filter.tr,
                                onPressed: () {
                                  if (currentTime == null) {
                                    ToastUtil.waring('如需指定过滤日期,请选择具体日期,否则重置');
                                    return;
                                  }
                                  GetHelper.back();
                                  filter?.call(
                                      DateUtil.formatDate(currentTime, format: DataFormats.y_mo_d));
                                },
                              ),
                            ),
                          ],
                        ),
                      ),
                      gapH36,
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  showDateTimePicker() {
    PDuration selectDate;
    if (currentTime != null) {
      selectDate =
          PDuration(year: currentTime!.year, month: currentTime!.month, day: currentTime!.day);
    } else {
      selectDate = PDuration(
          year: DateTime.now().year, month: DateTime.now().month, day: DateTime.now().day);
    }

    var pickerStyle = PickerStyle(
      showTitleBar: true,
      title: Container(
        alignment: Alignment.center,
        width: double.infinity,
        child: Text(MultiLanguage.selectDeliveryTime.tr, style: normalTextColor34),
      ),
      menuHeight: 36,
      cancelButton: Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 8.w),
        child: Text(MultiLanguage.cancel.tr, style: normalTextColor28),
      ),
      commitButton: Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 8.w),
        child: Text(MultiLanguage.determine.tr, style: primaryTextColor28),
      ),
      textSize: 36.sp,
      headDecoration: BoxDecoration(
          color: ThemeUtil.getTheme().colorScheme.secondary,
          borderRadius:
              const BorderRadius.only(topLeft: Radius.circular(8), topRight: Radius.circular(8))),
      textColor: ThemeUtil.getTheme().primaryColor,
      backgroundColor: ThemeUtil.getTheme().colorScheme.secondary,
      itemOverlay: CupertinoPickerDefaultSelectionOverlay(
          background: ThemeUtil.getTheme().normalTextColor().withOpacity(0.05)),
    );
    Pickers.showDatePicker(
      context,
      selectDate: selectDate,
      mode: DateMode.YMD,
      suffix: Suffix.normal(),
      minDate: PDuration(year: 2020, month: 2, day: 10),
      maxDate: PDuration(second: 22),
      pickerStyle: pickerStyle,
      onConfirm: (p) {
        currentTime = DateTime(p.year!, p.month!, p.day!);
        loadingTime = DateUtil.formatDate(currentTime, format: DataFormats.zh_y_mo_d);
        setState(() {});
      },
      // onChanged: (p) => print(p),
    );
  }
}
